<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="tableText['statisticsTime']">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="tableText['channelSource']">
          <el-select v-model="searchForm.channel" clearable :placeholder="$t('common.select')" @change="setSource">
            <el-option
              v-for="item in tableText['source']"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['promotionMethod']">
          <el-select v-model="searchForm.promotionTypeStrList" collapse-tags clearable multiple :placeholder="$t('usrModule.list.prompt.promotionMethod')">
            <el-option
              v-for="item in promotionData"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['referrer']">
          <el-input v-model="searchForm.referrerName" clearable :placeholder="$t('common.input')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          prop="date"
          :label="tableText['statisticsTime']"
          min-width="120"
        />
        <el-table-column
          prop="stageId"
          :label="tableText['stageId']"
          min-width="120"
        />
        <el-table-column
          prop="stageName"
          :label="tableText['stageName']"
          min-width="120"
        />
        <el-table-column
          prop="stageType"
          :label="tableText['stageType']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row.stageType === 0 ? tableText['js'] : tableText['rgsh'] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditCount"
          :label="tableText['trials']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditPassCount"
          :label="tableText['passes']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditPassCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="auditRefuseCount"
          :label="tableText['rejections']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['auditRefuseCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="closeCreditCountToday"
          :label="tableText['manualReviewClosed']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['closeCreditCountToday'] || 0 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>

</template>

<script>
import { getStageList, exportStageList, allPackage } from '@/api/risk'
import { formatDate } from '@/utils/method'
export default {
  name: 'PhaseData',
  data() {
    return {
      loading: true,
      tableData: [],
      allRes: [],
      time: '',
      allPackageData: [],
      promotionData: [],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.time = [new Date(), new Date()]
    this.tableText = this.$t('riskReport.phaseData')
    this.getData()
    this.getAllPackage()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        timeStart: null,
        timeEnd: null,
        channel: '',
        promotionTypeStrList: null,
        referrerName: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.time = [new Date(), new Date()]
      this.searchForm = this.params()
      this.getData()
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['timeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['timeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['timeStart'] = null
        data['timeEnd'] = null
      }
      return data
    },
    getAllPackage() {
      allPackage()
        .then(res => {
          if (res.success) {
            this.allPackageData = res.data
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getData() {
      this.loading = true
      getStageList(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    exportList() {
      exportStageList(this.searchData())
    },
    setSource() {
      const channel = this.searchForm.channel
      if (channel === 1) {
        const arr = this.tableText['method']
        this.searchForm.promotionTypeStrList = []
        arr.forEach(v => {
          this.searchForm.promotionTypeStrList.push(v['value'])
        })
        this.promotionData = arr
      } else if (channel === 2) {
        const arr = this.tableText['method1']
        this.searchForm.promotionTypeStrList = []
        arr.forEach(v => {
          this.searchForm.promotionTypeStrList.push(v['value'])
        })
        this.promotionData = arr
      } else if (channel === 3) {
        this.promotionData = []
        const arr = this.allPackageData.filter(v => {
          return v['mediaSource'] !== null
        })
        const node = []
        this.searchForm.promotionTypeStrList = []
        arr.forEach(v => {
          node.push({
            desc: v['mediaSource'],
            value: v['mediaSource']
          })
          this.searchForm.promotionTypeStrList.push(v['mediaSource'])
        })
        this.promotionData = node
      } else {
        this.promotionData = []
        this.searchModel.promotionTypeStrList = []
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
